package com.edu.dgut.controller;

import com.edu.dgut.dao.goodsRepository;
import com.edu.dgut.dao.ordertableRepository;
import com.edu.dgut.entity.goodsEntity;
import com.edu.dgut.entity.ordertableEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/")
@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
public class mainController {

    @Autowired
    private ordertableRepository ordertable;

    @Autowired
    private goodsRepository goods;

    //获取订单总数，订单已完成数和商品种类数
    @RequestMapping("show")
    public Map<String,Object> show(){
        Map<String,Object> map = new HashMap<>();
        List<goodsEntity> all_goods = goods.findAll();
        List<ordertableEntity> all_ordertable = ordertable.findAll();
        int finish = 0, i = 0;
        while(all_ordertable.size() > i){       //获取订单已完成数
            if(all_ordertable.get(i++).getState() == 1){
                finish++;
            }
        }
        map.put("all_ordertable",all_ordertable.size());  //订单总数
        map.put("finish",finish);                         //订单已完成数
        map.put("total",all_goods.size());                //商品种类数
        return map;
    }
}
